import * as Utils from "@/MCharts/utils";
import { roseOptions, roseSeriesItem } from "./roseOptions";
import { spacecgOptions, spacecgSeriesItem } from "./spacecgOptions";

function getCurrentSeries(optionsSeries, customSeries) {
  let result = optionsSeries;
  if (customSeries) {
    optionsSeries = { ...optionsSeries };
    customSeries = { ...customSeries };

    const seriesCollection = Utils.$m.mergeDeep(optionsSeries, customSeries);
    seriesCollection.length =
      Object.getOwnPropertyNames(seriesCollection).length;
    result = Array.from(seriesCollection);
  }

  return result;
}

function initOptions() {
  return {
    series: [],
    legend: {
      top: "4%",
    },
    tooltip: {
      trigger: "item",
      textAlign: "right",
      axisPointer: {
        type: "cross",
      },
    },
  };
}

const optionCollections = {
  Rose: {
    options: roseOptions,
    seriesItem: roseSeriesItem,
  },
  Spacecg: {
    options: spacecgOptions,
    seriesItem: spacecgSeriesItem,
  },
};
// import
export default {
  options: {},
  // 获取配置项
  getOptions(data, customOptions, type = "Basic") {
    const styleOptions = {};
    const styleOptionsType = optionCollections[type];
    styleOptions.options = styleOptionsType ? styleOptionsType.options : {};
    styleOptions.seriesItem = styleOptionsType
      ? styleOptionsType.seriesItem
      : {};
    this.options = initOptions();
    let options = { ...this.options, ...styleOptions.options };
    let series = [];
    if (data && data.length) {
      data.forEach((e) => {
        let seriesItem = {
          type: "pie",
          radius: ["50%", "64%"],
          areaStyle: {},
          ...styleOptions.seriesItem,
          ...e,
          name: e.name,
          data: [],
        };
        if (e.data && e.data.length) {
          e.data.forEach((el) => {
            seriesItem.data.push({
              name: el.name,
              value: el.value,
            });
          });
          series.push(seriesItem);
        }
      });
      // options.series = series
      customOptions = { ...customOptions };
      customOptions.series = getCurrentSeries(series, customOptions.series);
    }
    return Utils.$m.mergeDeep(options, customOptions);
  },
};
